package org.gcu.filestorage.serive;

import lombok.extern.slf4j.Slf4j;
import org.gcu.filestorage.mapper.ApplyInfoMapper;
import org.gcu.filestorage.pojo.ApplyRecord;
import org.gcu.filestorage.pojo.SpaceStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ApplyInfoServiceImpl implements ApplyInfoService {

    private final ApplyInfoMapper applyInfoMapper;

    @Autowired
    public ApplyInfoServiceImpl(ApplyInfoMapper applyInfoMapper) {
        this.applyInfoMapper = applyInfoMapper;
    }

    @Override
    public List<ApplyRecord> queryApplyRecord(String account) {
        return this.applyInfoMapper.queryApplyRecord(account);
    }

    @Override
    public SpaceStatus querySpaceInfo(String account) {
        return this.applyInfoMapper.querySpaceStatus(account);
    }

    @Override
    public boolean insertNewApplyRecord(String account, int applyType, int applyStatus, long applyVolume, String applyReason, long applyTime) {
        int count = this.applyInfoMapper.insertNewApplyRecord(account, applyType, applyStatus, applyVolume, applyReason, applyTime);
        return count > 0;
    }

    public boolean updateApplyRecord(int id, int applyStatus, String approvalDesc) {
        int count = this.applyInfoMapper.updateApplyRecord(id, applyStatus, approvalDesc);
        log.info("updateApplyStatus(), id=" + id + ", applyStatus=" + applyStatus + ",approvalDesc=" + approvalDesc + ", count=" + count);
        return count > 0;
    }

    public List<ApplyRecord> queryApplyRecordByStatus(int applyStatus) {
        return this.applyInfoMapper.queryApplyRecordByStatus(applyStatus);
    }


    @Override
    public boolean insertNewSpaceStatus(String account, long totalVolume, long usedVolume, long remainVolume) {
        int count = this.applyInfoMapper.insertNewSpaceStatus(account, totalVolume, usedVolume, remainVolume);
        return count > 0;
    }

    @Override
    public boolean updateSpaceStatus(String account, long totalVolume, long usedVolume, long remainVolume) {
        int count = this.applyInfoMapper.updateSpaceStatus(account, totalVolume, usedVolume, remainVolume);
        return count > 0;
    }
}
